/*
 *  tf_listmodel.h
 *  Perceel
 *
 *  Created by Nicola Gigante on 17/03/08.
 *  Copyright 2008 Nicola Gigante. All rights reserved.
 *
 */

#ifndef __TF_LIST_H__
#define __TF_LIST_H__

#include <QAbstractListModel>
#include <QList>
#include <QVariant>
#include <QMessageBox>
#include <QFile>
#include <QDomDocument>

#include "tf.h"
#include "exceptions.h"

class TFList : public QAbstractListModel
{
	Q_OBJECT
	
	QList<TF> functions;
	
	bool m_modified;
	QString m_filename;
	
public:
	TFList(QObject *parent = 0) : QAbstractListModel(parent),
										 m_modified(false) {}
	
	// Access methods
	void add(TF tf);
	void remove(int f);
	
	// File management methods
	bool modified() const { return m_modified; }
	
	bool isNew() const { return m_filename.isNull(); }
	
	QString filename() const { return m_filename; }
	void setFilename(QString f) { m_filename = f; }
	
	bool save() { return save(filename()); }
	bool save(QString filename);
	
	bool open(QString filename);
	
	
	// Model methods
	int rowCount(const QModelIndex & = QModelIndex()) const { return functions.count(); }
	QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
signals:
	void modified(bool m);

protected:
	// Read/Write functions
	QList<TF> read(QIODevice *d) throw(IOError*);
	bool write(QIODevice *d);
	
	
private:
	void setModified(bool m) { m_modified = m; emit modified(m); }
};

#endif
